package com.lanou.bbs.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JDBCUtil {
    public static final String URL = "jdbc:mysql:///bbs";
    public static final String USERName = "root";
    public static final String PASSWORD = "123456";

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");//官方固定写法,注册驱动
            //当调用这个JDBCUitl类时,这个静态代码块里的代码会自动的执行,即会自动的执行
            //这个代码,Driver也就被调用了,java封装了Driver的方法,在那里面他已经new了,所以
            //这块这样写就行,不需要再new一遍
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(URL, USERName, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
       return connection;
    }


}
